<template>
    <transition>
        <div style="pointer-events: auto;">
            <!--内容主题-->
            <div class="efomo-main-content">
                <h5 class="efomo-h5">{{$t('m.efomo.income.tit')}} <span
                        @click="aShowTip({id:'6',explainShow:true})">?</span></h5>
                <div class="efomo-tit-asset">
                    <span>{{ fomoInfo.expect }}</span>ETSC
                    <p style="margin-top: 10px;color: #e5bc8e;">{{$t('m.efomo.biling.content.inputBox.text')[2]}}:&nbsp;
                        {{
                        balance.ETSC | roundOff(2) }}
                        ETSC</p>
                </div>
                <div class="efomo-income-tab">
                    <ul class="efomo-income-list">
                        <li>
                            <p class="income-tit">{{$t('m.efomo.income.type')[0]}}</p>
                            <p class="income-price">{{ fomoInfo.investH }}</p>
                            <a v-if="fomoInfo.investH > 0" @click="cancelPre('cancel')" href="javascript:void(0);">{{$t('m.efomo.btn.ransom')}}</a>
                        </li>
                        <li>
                            <p class="income-tit">{{$t('m.efomo.income.type')[1]}}</p>
                            <p class="income-price">{{ fomoInfo.investD }}</p>
                            <a href="javascript:void(0);"></a>
                        </li>
                        <li>
                            <p class="income-tit">{{$t('m.efomo.income.type')[2]}}</p>
                            <p class="income-price font18">{{ fomoInfo.withdrawH }}
                                <span class="efomo-circle">
                                    <span>{{$t('m.efomo.income.status')[0]}}</span>
                                </span>
                            </p>
                            <p class="income-price font18">{{ fomoInfo.awardLaffH }}
                                <span class="efomo-circle">
                                    <span>{{$t('m.efomo.income.status')[1]}}</span>
                                </span>
                            </p>
                        </li>
                        <li>
                            <p class="income-tit">{{$t('m.efomo.income.type')[3]}}</p>
                            <p class="income-price font18">{{ fomoInfo.withdrawD }}
                                <span class="efomo-circle">
                                    <span>{{$t('m.efomo.income.status')[0]}}</span>
                                </span>
                            </p>
                            <p class="income-price font18">{{ fomoInfo.awardLaffD }}
                                <span class="efomo-circle">
                                    <span>{{$t('m.efomo.income.status')[1]}}</span>
                                </span>
                            </p>
                        </li>
                    </ul>
                </div>
                <div class="efomo-main-tip">
                    <p>{{$t('m.efomo.tips')[1]}}</p>
                </div>
                <div class="efomo-main-btn efomo-income-btn">
                    <!--领取活期奖励，投资额大于0且到达领取时间-->
                    <button v-if="fomoInfo.investH == 0" style="background-color: #97989a;border-color: #97989a">
                        <span>{{$t('m.efomo.income.type')[4] }}</span>
                    </button>
                    <button v-else-if="$parent.hCD.time > 0" style="background-color: #97989a;border-color: #97989a">
                        <span>{{$parent.hCD.h }}:{{ $parent.hCD.m }}:{{ $parent.hCD.s }}</span>
                    </button>
                    <button @click="getBankInterestPre(1)" v-else>{{ $t('m.efomo.income.type')[4] }}</button>

                    <!--领取定期奖励，预计产矿值大于0且到达领取时间-->
                    <button v-if="fomoInfo.expect == 0" style="background-color: #97989a;border-color: #97989a">
                        <span>{{$t('m.efomo.btn.income')[0]}}</span>
                    </button>
                    <button v-else-if="$parent.dCD.time > 0" style="background-color: #97989a;border-color: #97989a">
                        <span>{{$parent.dCD.h }}:{{ $parent.dCD.m }}:{{ $parent.dCD.s }}</span>
                    </button>
                    <button @click="getBankInterestPre(0)" v-else>
                        <span>{{ $t('m.efomo.btn.income')[0] }}</span>
                    </button>
                </div>
            </div>
            <!--底部列表-->
            <div v-if="interestList.length > 0" class="efomo-main-foot">
                <div class="efomo-main-list">
                    <h5>{{$t('m.efomo.list.tit')[1]}}</h5>
                    <ul class="efomo-list-type">
                        <li>{{$t('m.efomo.list.type')[0]}}</li>
                        <li>{{$t('m.efomo.list.type')[1]}}</li>
                        <li>{{$t('m.efomo.list.type')[2]}}</li>
                        <li>{{$t('m.efomo.list.type')[3]}}</li>
                    </ul>
                    <ul v-for="v in interestList" v-if="v.type != 4" class="efomo-list-item">
                        <li>{{ v.timestamp | time2DateFomo }}</li>
                        <li>
                            <template v-if="v.type == 0">
                                <!--{{$t('m.efomo.biling.content.type')[0]}}-->
                                {{$t('m.efomo.income.type')[5]}}
                            </template>
                            <template v-else-if="v.type == 1">
                                <!--{{$t('m.efomo.biling.content.type')[1]}}-->
                                {{$t('m.efomo.income.type')[6]}}
                            </template>
                            <template v-else-if="v.type == 2">
                                <!--{{$t('m.efomo.biling.content.type')[1]}}-->
                                {{$t('m.efomo.income.type')[7]}}
                            </template>
                            <template v-else-if="v.type == 3">
                                <!--{{$t('m.efomo.biling.content.type')[1]}}-->
                                {{$t('m.efomo.income.type')[8]}}
                            </template>
                        </li>
                        <li class="price">{{ v.eth }}</li>
                        <li>
                            <template v-if="v.status == 0">{{$t('m.efomo.success')[2]}}</template>
                            <template v-else>{{$t('m.efomo.success')[0]}}</template>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </transition>
</template>

<script>
    import '@/assets/css/efomo.scss'
    import axios from 'axios';
    import {myAlert} from "@/assets/js/h5plus/nativeUI";
    import {investCancel, getBankInterest} from '@/assets/js/etherjs/fomo/fomo'
    import {AESDecrypt} from '@/assets/js/utils/crypto'
    import {getInit} from '@/assets/js/etherjs/fomo/fomo'

    export default {
        name: "efomo_income",
        data() {
            return {
                pathName: 'income',
                showPullingDown: false,
                minInvest: 100,
                interestList: [],
                request: {
                    pageNo: 1,
                    pageSize: 20,
                    address: "",
                    isEnd: false,
                },
            }
        },
        mounted() {
            // 设置头部
            this.$parent.title = this.$t('m.efomo.title')[2];
            this.$parent.init();
        },
        watch: {
            prompt(v) {
                if (v.isConfirm) {
                    let pKey = AESDecrypt({
                        pwd: v.value,
                        msg: this.walletActive.pKey
                    });
                    if (pKey == '') {
                        myAlert({
                            msg: this.$t('m.wallet.manager.index.prompt[7]'),
                            button: this.$t('m.wallet.manager.index.prompt[4]')
                        });
                        return
                    }
                    this.aWalletDecode({
                        pKey: pKey
                    });
                    if (v.complete == 'cancel') {
                        // computed计算到的变化，不及时？
                        this.cancel();
                    }
                    if (v.complete == 'getBankInterestPre_1') {
                        // computed计算到的变化，不及时？
                        this.getBankInterest(1);
                    }
                    if (v.complete == 'getBankInterestPre_0') {
                        // computed计算到的变化，不及时？
                        this.getBankInterest(0);
                    }
                }
            }
        },
        methods: {
            init() {
                // 获取奖励列表
                this.request.pageNo = 1;
                this.request.isEnd = false;
                this.request.to = this.walletActive.address;
                this.request.bID = this.fomoInfo.bID;
                this.interestList = [];
                return this.getInterestList();
            },
            changeTab(url, path) {
                this.active = path;
                this.changeRoute(url);
            },

            cancelPre(v) {
                if (this.walletDecode.pKey == "") {
                    this.aPrompt({
                        show: true,
                        title: this.$t('m.my.identity.prompt[7]'),
                        cancel: this.$t('m.my.identity.prompt[8]'),
                        confirm: this.$t('m.my.identity.prompt[6]'),
                        type: "password",
                        placeholder: this.$t('m.my.identity.prompt[7]'),
                        complete: v
                    })
                } else {
                    this.cancel();
                }
            },
            async cancel() {
                console.log("正在赎回");
                this.plusDiff(() => {
                    plus.nativeUI.toast(this.$t('m.efomo.toast[9]'), {verticalAlign: 'center'});
                });
                let res = await investCancel({
                    privateKey: this.walletDecode.pKey,
                });
                if (res == 0) {
                    console.log("赎回失败");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[10]'), {verticalAlign: 'center'});
                    });
                } else {
                    // 先刷新 fomoInfo数据
                    await getInit(this.walletActive.address);
                    console.log("赎回成功");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[11]'), {verticalAlign: 'center'});
                    });
                }
            },
            getBankInterestPre(type) {
                if (this.walletDecode.pKey == "") {
                    this.aPrompt({
                        show: true,
                        title: this.$t('m.my.identity.prompt[7]'),
                        cancel: this.$t('m.my.identity.prompt[8]'),
                        confirm: this.$t('m.my.identity.prompt[6]'),
                        type: "password",
                        placeholder: this.$t('m.my.identity.prompt[7]'),
                        complete: "getBankInterestPre_" + type
                    })
                } else {
                    this.getBankInterest(type);
                }
            },
            async getBankInterest(type) {
                console.log("正在获取收益");
                this.plusDiff(() => {
                    plus.nativeUI.toast(this.$t('m.efomo.toast[12]'), {verticalAlign: 'center'});
                });
                let res = await getBankInterest({
                    privateKey: this.walletDecode.pKey,
                    type
                });
                if (res == 0) {
                    console.log("获取收益失败");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[13]'), {verticalAlign: 'center'});
                    });
                } else {
                    // 先刷新 fomoInfo数据
                    await getInit(this.walletActive.address);
                    console.log("获取收益成功");
                    this.plusDiff(() => {
                        plus.nativeUI.toast(this.$t('m.efomo.toast[14]'), {verticalAlign: 'center'});
                    });
                    this.$parent.synBalanceOnce();
                }
            },
            getInterestList() {
                if (this.request.isEnd) return new Promise((resolve) => {
                    resolve(1);
                });
                return axios.get(this.requestConfig.apiHost + "/client/fomo/interest/list", {
                    params: this.request
                }).then((response) => {
                    if (response.data.error_code != 0) {
                        throw new Error("get list error");
                    }
                    let list = response.data.data.list;
                    this.interestList = this.interestList.concat(list);
                    if (list.length < 15) this.request.isEnd = true;
                }).catch((error) => {
                    myAlert({
                        msg: this.$t('m.efomo.toast[15]'),
                        button: this.$t('m.wallet.transaction.list.prompt[9]')
                    });
                    console.log(error);
                });
            },
            getMore() {
                this.request.pageNo++;
                return this.getInterestList();
            },
        }
    }
</script>

<style lang="scss">

</style>